﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for clsFileUpLoad
/// </summary>
/// 
namespace Dami
{
    public class clsFileUpLoad
    {
        public clsFileUpLoad()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public List<String[]> parseCSV(String path)
        {
            //TextReader 
            List<string[]> parseData = new List<string[]>();
            path = System.Web.HttpContext.Current.Server.MapPath("~/" + path);
            try {
                using (StreamReader readFile = new StreamReader(path))
                {
                    string line;
                    string[] row;
                    while ((line = readFile.ReadLine()) != null)
                    {
                        row = line.Split(',');
                        parseData.Add(row);
                    }
                }
            }
            catch(Exception e)
            {

            }
            return parseData;
        }

        public static Boolean CheckPath(String path) {
            Boolean bBool = false;
            int index = path.IndexOf("http");
            if (index != -1)
                return true;
            try
            {
                String filePath = System.Web.HttpContext.Current.Server.MapPath("~/" + path) + "/";
                if (File.Exists(filePath))
                {
                    bBool = true;
                }
            }
            catch { 
            }

            return bBool;
        }

        public void DeleteFile(String path, string fileName)
        {
            try
            {
                String filePath = System.Web.HttpContext.Current.Server.MapPath("~/" + path) + "/";
                filePath = filePath + fileName;
                System.IO.File.Delete(filePath);
            }
            catch
            {

            }
        }

        
        private static int IndexTag(String s,String tag) {
            int i = -1;
            i = s.IndexOf(tag);
            return i;
        }

        private static String ChangeString(String s,String tag) {
            int k = -1;
            int begin = -1;
            int end = -1;
            begin = IndexTag(s, tag);
            int dem = 0;
            if (begin != -1)
            {
                for (k = begin; k < s.Length; k++)
                {
                    dem++;
                    if (s[k].ToString() == ">" || s[k].ToString() == "/>")
                    {
                        end =k;
                        break;
                    }
                }
            }
            if (begin < end-1) {
                String str = "";
                str = s.Substring(begin,dem);
                if (!String.IsNullOrEmpty(str)) {
                    s = s.Replace(str, "");
                }
            }
            return s;
        }
        public static String DeHtml(String s) {
            
            if (!string.IsNullOrEmpty(s)) {
                s = s.Trim();
                s = s.Replace("</head>", "").Replace("</div>", "").Replace("<head>", "");
                s = ChangeString(s,"<div");
                s = ChangeString(s, "<p");
                s = ChangeString(s, "<img");
                s = ChangeString(s, "<h1");
                s = ChangeString(s, "<h2");
                s = ChangeString(s, "<h3");
                s = ChangeString(s, "<h4");
                s = ChangeString(s, "<h5");
                s = ChangeString(s, "<h6");
                s = s.Replace("</div>", "").Replace("</p>", "").Replace("</h2>", "").Replace("</h3>", "").Replace("/>", "");
                s = s.Replace("</h4>", "").Replace("</h5>", "").Replace("</h6>", "").Replace(">","");
            }
            return s;
        }

        public static string HtmlEncode(string html)
        {
            if (html == null)
            {
                throw new ArgumentNullException("html");
            }
            // replace & by &amp; but only once!
            Regex rx = new Regex("&(?!(amp;)|(lt;)|(gt;)|(quot;))", RegexOptions.IgnoreCase);
            return rx.Replace(html, "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;");
        }


        /// <summary>
        /// Ham xoa file
        /// </summary>
        /// <param name="fileName"></param>
        public void DeleteFile( string fileName)
        {
            try
            {
                String filePath = System.Web.HttpContext.Current.Server.MapPath("~/") + fileName.Trim();
                System.IO.File.Delete(filePath.Trim());
            }
            catch
            {

            }
        }

        /// <summary>
        /// Download file
        /// </summary>
        /// <param name="response"></param>
        /// <param name="fileName"></param>
        /// <param name="fileSize"></param>
        /// <param name="contentType"></param>
        /// <param name="buffFile"></param>
        public void Download(HttpResponse response, String fileName, decimal fileSize, String contentType, Byte[] buffFile)
        {
            //Response.AddHeader("content-disposition", "inline; filename=" + "InfonationOut.pdf");
            response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}",fileName));
            response.AddHeader("Content-Length",fileSize.ToString());
            response.ContentType = contentType;
            response.BinaryWrite(buffFile);
        }

        /// <summary>
        /// Luu file duoi dang []byte
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public byte[] buffFile(FileUpload file) {
            byte[] buff = new byte[file.PostedFile.ContentLength];
            file.PostedFile.InputStream.Read(buff, 0, file.PostedFile.ContentLength);
            return buff;
        }

        /// <summary>
        /// Ham luu file
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
  
        public String FileSaveImage(String path, FileUpload fileName)
        {
            String saveFile =path + "/";
            String filePath = System.Web.HttpContext.Current.Server.MapPath("~/" + path) + "/";
            if (fileName.FileName == "")
            {
                return "";
            }
            String str = "";
            if (fileName.HasFile)
            {
                String strNameMusic = fileName.FileName;
                strNameMusic = strNameMusic.ToLower();

                int i = -1;
                i = strNameMusic.LastIndexOf(".");
                try
                {
                    str = strNameMusic.Substring(i);
                   // if (str.ToLower() == ".jpg" || str.ToLower() == ".jpeg" || str.ToLower() == ".gif" || str.ToLower() == ".png" || str.ToLower() == ".bmp")
                  //  {
                        str = System.Guid.NewGuid().ToString() + str;
                        str = DateTime.Now.ToString("yyyy_MM_dd") + "_" + str;
                        filePath = filePath + str;// fileName.FileName;
                        HttpPostedFile tenFile = fileName.PostedFile;
                        fileName.SaveAs(filePath);
                        
                   // }
                }
                catch
                {
                    str = "";
                }
            }
            if (str != "")
            {
                
                str = saveFile + str;
            }
            return str;
        }

    /// <summary>
    /// Hàm lưu các dạng file nhạc 
    /// </summary>
    /// <param name="path"></param>
    /// <param name="fileName"></param>
    /// <returns></returns>

        public String FileSave(String path, FileUpload fileName)
        {
            String saveFile = "~/" + path + "/";
            String filePath = System.Web.HttpContext.Current.Server.MapPath("~/" + path) + "/";
            if (fileName.FileName == "")
            {
                return "";
            }
            String str = "";
            if (fileName.HasFile)
            {
                String strNameMusic = fileName.FileName;
                strNameMusic = strNameMusic.ToLower();

                int i = -1;
                i = strNameMusic.IndexOf(".mp3".ToLower());
                try
                {
                    str = strNameMusic.Substring(i);
                    if (str.ToLower() == ".mp3".ToLower())
                    {
                        str = System.Guid.NewGuid().ToString() + str;
                        str = DateTime.Now.ToString("yyyy_MM_dd") + "_" + str;
                        filePath = filePath + str;// fileName.FileName;
                        HttpPostedFile tenFile = fileName.PostedFile;
                        fileName.SaveAs(filePath);
                        saveFile += fileName.FileName;
                    }
                }
                catch
                {
                    str = path+str;
                }
            }
            return str;
        }

    }
}